# 构建阶段
FROM node:18-alpine AS builder

WORKDIR /app

# 先复制包管理文件，利用缓存层
COPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile

# 复制源代码并构建
COPY . .
RUN yarn build:prod

# 提取 API 地址供运行阶段使用
RUN grep '^VITE_BASE_API=' .env.production | sed 's/^VITE_BASE_API=[ \t]*//; s/[ \t]*$//; s/^"//; s/"$//' | tr -d '\r' > /app/.api_target

# 运行阶段：nginx 服务静态文件
FROM nginx:stable-alpine

# 复制构建产物到 nginx 默认目录
COPY --from=builder /app/dist /usr/share/nginx/html

# 复制 API 目标地址文件和配置文件
COPY --from=builder /app/.api_target /etc/nginx/.api_target
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# 暴露 80 端口
EXPOSE 80

ENTRYPOINT ["/entrypoint.sh"]
